package com.jy.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jy.common.ResultInfo;
import com.jy.entity.Area;
import com.jy.service.IAreaService;
import com.jy.vo.PlateJsonResult;
import com.jy.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车位区域表 前端控制器
 * </p>
 *
 * @author xiexingxin
 * @since 2022-11-15
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private IAreaService iAreaService;

    /**
     * 获取车位区域信息
     *
     * @return
     */
    @GetMapping("/getAllInfo")
    public Result getAllInfo() {
        List<Area> allInfo = iAreaService.getAllInfo();
        if (allInfo.isEmpty()) {
            return Result.failed(ResultInfo.SELECT_SUCCESS);
        }
        return Result.success(allInfo);
    }

    /**
     * 添加车位区域信息
     *
     * @param area
     * @return
     */
    @PostMapping("/addareainfo")
    public Result addAreaInfo(@RequestBody Area area) {
        boolean save = iAreaService.save(area);
        return save == true ? Result.success(ResultInfo.ADD_SUCCESS, save) : Result.failed(ResultInfo.ADD_FAIL);
    }

    /**
     * 修改车位区域信息
     *
     * @param aredId
     * @return
     */
    @DeleteMapping("/delareainfo")
    public Result delAreaInfo(Integer aredId) {
        boolean b = iAreaService.removeById(aredId);
        return b == true ? Result.success(ResultInfo.DELETE_SUCCESS, b) : Result.failed(ResultInfo.DELETE_FAIL);
    }

    /**
     * 根据Id修改车位区域信息
     *
     * @param area
     * @return
     */
    @PostMapping("/updareainfo")
    public Result updAreaInfo(@RequestBody Area area) {
        if (area.getAName().isEmpty()) {
            return Result.failed(ResultInfo.POST_NAME_ISNOTNULL);
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("areaId", area.getAreaId());
        updateWrapper.set("a_name", area.getAName());
        updateWrapper.set("t_upanddown", area.getTUpanddown());
        boolean update = iAreaService.update(updateWrapper);
        return update == true ? Result.success(ResultInfo.UPDATE_SUCCESS, update) : Result.failed(ResultInfo.UPDATE_FAIL);
    }

}
